package com.region.integral.hibernate;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;

import com.region.integral.business_user.pojo.BLevelEntity;
import com.region.integral.business_user.pojo.BUIEntity;
import com.region.integral.common_user.pojo.CLevelEntity;
import com.region.integral.common_user.pojo.CUIEntity;
import com.region.integral.common_user.pojo.KCurrencyEntity;
import com.region.integral.common_user.pojo.LimitEntity;
import com.region.integral.exception.IntegralException;
import com.region.integral.util.ExceptionPropertiesUtil;
import com.region.integral.util.IntegralExceptionConstants;

public class BaseHibernate {
	@Resource
	protected HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	protected void validate(int userid)throws IntegralException {
		if (userid == 0) {
			throw new IntegralException(
					ExceptionPropertiesUtil
							.getExceptionMessage(IntegralExceptionConstants.USERID_IS_ZERO));
		}
		if (hibernateTemplate == null) {
			throw new IntegralException(
					ExceptionPropertiesUtil
							.getExceptionMessage(IntegralExceptionConstants.SYSTEM_ERROR));
		}

	}

	protected CUIEntity getCUIEntity(int userid)throws IntegralException {
		validate(userid);
		List<CUIEntity> list = hibernateTemplate
				.find("FROM CUIEntity WHERE userid = " + userid);
		if (list.isEmpty()) {
			throw new IntegralException(
					ExceptionPropertiesUtil
							.getExceptionMessage(IntegralExceptionConstants.USER_NOT_EXIST));
		}
		CUIEntity cuiEntity = list.get(0);
		return cuiEntity;
	}
	protected KCurrencyEntity getKCurrencyEntity(int userid)throws IntegralException {
		validate(userid);
		List<KCurrencyEntity> list = hibernateTemplate
				.find("FROM KCurrencyEntity WHERE userid = " + userid);
		if (list.isEmpty()) {
			throw new IntegralException(
					ExceptionPropertiesUtil
							.getExceptionMessage(IntegralExceptionConstants.USER_NOT_EXIST));
		}
		KCurrencyEntity kcEntity = list.get(0);
		return kcEntity;
	}
	protected BUIEntity getBUIEntity(int userid)throws IntegralException {
		validate(userid);
		List<BUIEntity> list = hibernateTemplate
				.find("FROM BUIEntity WHERE userid = " + userid);
		if (list.isEmpty()) {
			throw new IntegralException(
					ExceptionPropertiesUtil
							.getExceptionMessage(IntegralExceptionConstants.USER_NOT_EXIST));
		}
		BUIEntity buiEntity = list.get(0);
		return buiEntity;
	}
	protected LimitEntity getLimitEntity(int userid)throws IntegralException {
		validate(userid);
		List<LimitEntity> list = hibernateTemplate
				.find("FROM LimitEntity WHERE userid = " + userid);
		if (list.isEmpty()) {
			throw new IntegralException(
					ExceptionPropertiesUtil
							.getExceptionMessage(IntegralExceptionConstants.USER_NOT_EXIST));
		}
		LimitEntity limitEntity = list.get(0);
		return limitEntity;
	}
	protected BLevelEntity getBlevelEntity(String levelCode )throws IntegralException {
		List<BLevelEntity> list = hibernateTemplate
				.find("FROM BLevelEntity WHERE levelCode = '" + levelCode+"'");
		if (list.isEmpty()) {
			throw new IntegralException(
					ExceptionPropertiesUtil
							.getExceptionMessage(IntegralExceptionConstants.LEVEL_NOT_EXIST));
		}
		BLevelEntity bLevelEntity = list.get(0);
		return bLevelEntity;
	}
	protected CLevelEntity getClevelEntity(String levelCode )throws IntegralException {
		List<CLevelEntity> list = hibernateTemplate
				.find("FROM CLevelEntity WHERE levelCode = '" + levelCode+"'");
		if (list.isEmpty()) {
			throw new IntegralException(
					ExceptionPropertiesUtil
							.getExceptionMessage(IntegralExceptionConstants.LEVEL_NOT_EXIST));
		}
		CLevelEntity cLevelEntity = list.get(0);
		return cLevelEntity;
	}
}
